/**
 * @file Comment state
 * @module store/comment
 * @author Surmon <https://github.com/surmon-china>
 */

// import { Module, MutationTree, ActionTree } from 'vuex'
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators';

import QueryConstants from 'src/constants/query';
import { Pagination } from 'src/types/pagination';
import { Comment } from 'src/models/comment';
import { QueryPageParams } from 'src/types/requestParam';
import commentApi from 'src/api/comment/index';
import { Reply } from 'src/models/reply';
import Vuex from 'vuex';
import { localStorage } from 'src/adapter/localstorage';
// const store = new Vuex.Store<Comment>({})

const getDefaultListData = () => {
  return {
    data: [] as Comment[],
    pagination: QueryConstants.PaginationConst as Pagination
  };
};

@Module<Comment>({
  stateFactory: true,
  namespaced: true
  // name: 'comment',namespaced: false,
  // dynamic:true,
  // store:store,
})
export default class CommentStateModule extends VuexModule {
  //数据获取中
  fetching = false as boolean;

  //提交中
  posting = false as boolean;

  //评论总量
  totalCount = 0 as number;

  //回复总量
  totalReplyCount = 0 as number;

  //排序关键字
  sortKey = QueryConstants.SortType.Newest as number;

  //评论列表
  comments = getDefaultListData();

  //评论框展示
  commentPenPosition: any = {
    rootType: 'article', //comment,reply
    id: 0,
    quote: undefined
  };

  postContent: string = '';

  likeHistory: Array<number> = [];

  get disabled(): boolean {
    return this.fetching || this.posting;
  }

  @Mutation
  setCommentPenPosition(val: any) {
    this.commentPenPosition = val;
  }

  @Mutation
  setTotalCount(val: number) {
    this.totalCount = val;
  }

  @Mutation
  setTotalReplyCount(val: number) {
    this.totalReplyCount = val;
  }

  @Mutation
  SetTotal(total: number) {
    this.comments.pagination.total = total;
  }

  @Mutation
  SetFetching(fetching: boolean) {
    this.fetching = fetching;
  }

  @Mutation
  SetPosting(posting: boolean) {
    this.posting = posting;
  }

  @Mutation
  SetListData(comments: any) {
    this.comments.data = comments;
    this.fetching = false;
  }

  @Mutation
  ClearListData() {
    this.comments.data = [];
    this.comments.pagination.total = 0;
  }

  @Mutation
  SetCommentExpand(comment: Comment) {
    comment.expandAll = !comment.expandAll;
  }

  @Mutation
  ClearInput() {
    this.postContent = '';
  }
  @Mutation
  SetInput(val: string) {
    this.postContent = val;
  }

  @Mutation
  SET_LIKEHISTORY(history: number[]) {
    this.likeHistory = history;
  }

  @Mutation
  LIKECOMMENT(id: number) {
    if (this.likeHistory.find(n => n == id) == null) {
      this.likeHistory.push(id);
      localStorage.setItem('COMMENT_LIKE_HISTORY', this.likeHistory);
    }
  }

  get SortKey() {
    //   return this.wheels / 2
    return this.sortKey;
  }

  @Action({ rawError: true })
  async FetchList(params: QueryPageParams) {
    // 修正参数
    params = { ...QueryConstants.PaginationConst, ...params };
    // this.SetFetching(true);
    this.context.commit('SetFetching', true);
    let res = (await commentApi.getComments(params)) as any;

    this.context.commit('SetFetching', false);

    this.context.commit('SetListData', res.data.list);

    this.context.commit('SetTotal', res.data.pagination.total);
    return res;
  }
  @Action({ rawError: true })
  async PostComment(comment: Comment) {
    this.context.commit('SetPosting', true);
    const res = (await commentApi.postComment(comment).catch()) as any;
    this.context.commit('SetPosting', false);
    this.context.commit('ClearInput');
    return res;
  }

  @Action({ rawError: true })
  async Like(id: number): Promise<any> {
    this.context.commit('SetPosting', true);
    const res = (await commentApi.likeComment(id).catch()) as any;
    this.context.commit('SetPosting', false);
    return new Promise((resolve, reject) => {
      if (res.success) {
        console.log(res);
        this.context.commit('LIKECOMMENT', id);
        resolve(res.data);
      } else reject(false);
    });
  }

  //点赞|取消赞
  // @Action
  // async CommentLike(id:number,isLike:boolean=true){

  // }

  // get axles() {
  // //   return this.wheels / 2
  // }
}
